/**
 * 项目管理
 */
const getSession = (key, defaultValue = null) => {
  const value = sessionStorage.getItem(key);
  if (value) {
    return JSON.parse(value);
  } else {
    return defaultValue;
  }
};

export default {
  state: {
    globalProjectId: getSession("PROJECT_ID"),
    globalProjectList: [],
    globalProject: getSession("PROJECT"),
  },
  mutations: {
    SET_GLOBAL_PROJECT_ID(state, val) {
      sessionStorage.setItem("PROJECT_ID", JSON.stringify(val));
      state.globalProjectId = val;
    },
    SET_GLOBAL_PROJECT_LIST(state, val) {
      state.globalProjectList = val;
    },
    SET_GLOBAL_PROJECT(state, val) {
      sessionStorage.setItem("PROJECT", JSON.stringify(val));
      state.globalProject = val;
    },
  },
  actions: {
    SetGlobalProjectId({ commit }, val) {
      commit("SET_GLOBAL_PROJECT_ID", val);
    },
    SetGlobalProjectList({ commit }, val) {
      commit("SET_GLOBAL_PROJECT_LIST", val);
    },
    SetGlobalProject({ commit }, val) {
      commit("SET_GLOBAL_PROJECT", val);
    },
  },
  getters: {
    globalProjectId: (state) => state.globalProjectId,
    globalProjectList: (state) => state.globalProjectList,
    globalProject: (state) => state.globalProject,
  },
};
